package com.powerManager.sdk.handlers;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.jdbc.core.JdbcTemplate;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.powerManager.sdk.entity.MenuButtonEntity;
import com.google.gson.reflect.TypeToken;
import com.powerManager.sdk.common.JdbcSingleton;
import com.powerManager.sdk.common.PagingResult;
import com.powerManager.sdk.entity.MenuButtonEntity;
import com.powerManager.sdk.utils.JsonUtil;

/** 
* <p>Title: RoleHandler</p> 
* <p>Description:角色处理类 </p> 
* <p>Company: hnii</p> 
* @author    LiuC
* @date      2012-8-25 
*/ 
public class RoleHandler {

	/** 
	* <p>Title: loadListJSON</p> 
	* <p>Description: 返回角色列表json</p> 
	* @param request
	* @param response
	* @return
	* @throws ServletException
	* @throws IOException 
	*/ 
	public String loadListJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("loadListJson");
		String page = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");

		StringBuffer sql = new StringBuffer(
				"select role_id as RoleID,role_name as RoleName,remark as RoleDesc from sys_role");
		PagingResult pr = new PagingResult(sql.toString());
		String json = null;
		try {
			json = pr.pagingToJson(Integer.parseInt(pageSize), Integer.parseInt(page));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("json结果："+json);
		return "text:"+json;
	}
	
	
	/** 
	* <p>Title: addRole</p> 
	* <p>Description:新增角色 </p> 
	* @param request
	* @param response
	* @return 
	*/ 
	@SuppressWarnings("static-access")
	public String addRole(HttpServletRequest request, HttpServletResponse response){
		JdbcSingleton jdbc = new JdbcSingleton();
		//角色名称
		String roleName = request.getParameter("RoleName");
		//角色描述
		String roleDesc = request.getParameter("RoleDesc");
		if (checkUserExit(roleName)) {
			return "text:false";
		}
		String sql = "insert into sys_role(role_id,role_name,remark,status) values (GET_SEQ('SEQ_ROLE','0'),?,?,'1')";
		Object[] params = new Object[] { roleName, roleDesc };
		
		int result = jdbc.getJdbcTemplate().update(sql, params);
		
		return result>0?"text:"+"true":"text:"+"false";
	}
	
	
	/** 
	* <p>Title: updateRole</p> 
	* <p>Description: 更改角色信息</p> 
	* @param request
	* @param response
	* @return 
	*/ 
	@SuppressWarnings("static-access")
	public String  updateRole(HttpServletRequest request, HttpServletResponse response){
		JdbcSingleton jdbc = new JdbcSingleton();
		// 角色名称
		String roleName = request.getParameter("RoleName");
		// 角色描述
		String roleDesc = request.getParameter("RoleDesc");
		// 角色id
		String roleId = request.getParameter("RoleId");
		
		String sql="update sys_role set role_name=?,remark=? where role_id=?";
		Object[] params = new Object[]{roleName,roleDesc,roleId};
		
		int result = jdbc.getJdbcTemplate().update(sql, params);
		
		return result>0?"text:"+"true":"text:"+"false";
	}
	
	/** 
	* <p>Title: deleteRole</p> 
	* <p>Description: 删除角色信息</p> 
	* @param request
	* @param response
	* @return 
	*/ 
	@SuppressWarnings("static-access")
	public String deleteRole(HttpServletRequest request, HttpServletResponse response){
		JdbcSingleton jdbc = new JdbcSingleton();
		//获取角色Id
		String roleId = request.getParameter("RoleId");
		String sql = "delete from sys_role where role_id=?";
		Object[] params = new Object[]{roleId};
		
		int result = jdbc.getJdbcTemplate().update(sql, params);
		
		return result>0?"text:"+"true":"text:"+"false";
	}
	
	/** 
	* <p>Title: checkUserExit</p> 
	* <p>Description: 根据角色名称判断角色是否存在</p> 
	* @param roleName <b>角色名称</b>
	* @return 
	*/ 
	@SuppressWarnings("static-access")
	public boolean checkUserExit(String roleName) {
		JdbcSingleton jdbc = new JdbcSingleton();
		String sql = "select count(*) from sys_role where role_name=?";
		Object[] params = new Object[] { roleName };

		int i = jdbc.getJdbcTemplate().queryForInt(sql, params);
		return i > 0 ? true : false;
	}
	
	
	
	
	
	
}
